<template>
  <div class="login">
    <div class="loginForm">
      <h2 class="title">badBoy后台登录系统</h2>
      <el-form
        :model="ruleForm"
        status-icon
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            type="password"
            v-model="ruleForm.pwd"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm"
            >登录</el-button
          >
          <el-button @click="resetForm">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { http } from '../../utils/http/index'
import { useRouter } from 'vue-router'
import { reactive } from "vue";
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus';
const router = useRouter()
const store = useStore()
let ruleForm = reactive({
  username: "",
  pwd: ""
});
const submitForm = async () => {
  let res = await http.post('/user/login', ruleForm)
  if(res.code === 300){
    ElMessage.error({
      type: 'error',
      message: '账号或密码错误'
    })
    return 
  }
  store.dispatch('userMsg', res.msg);
  localStorage.setItem('token', res.token)
  router.push({ 'name': 'AdminArticle' })
}
const resetForm = () => {
  ruleForm.pass = ''
  ruleForm.checkPass = ''
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-size: 100% 100%;
  background-image: url("../../assets/guidao.jpeg");
  display: flex;
  justify-content: center;
  align-items: center;
  .loginForm {
    width: 400px;
    height: 400px;
    background-color: skyblue;
    border-radius: 10px;
    opacity: 0.8;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .title{
      margin-bottom: 30px;
    }
  }
}
</style>